import { getNoFbaAddressListApi } from "@/api/client/configCenter/addressManagement";
import { message } from "ant-design-vue";
export default function OperationHook(state: Base.GeneraDataType) {
  const { tableData, loading, emit, tableRef, selectRow, passData, searchForm } = state;
  const searchHandle = () => {
    const { name, postcode } = searchForm;
    const params: any = {
      name,
      postcode,
      pageNum: 1,
      pageSize: 999999,
      status: 1
    };
    loading.value = true;
    return getNoFbaAddressListApi(params as ClientAddressManagement.SearchForm)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          const row = tableData.value.find((t: any) => t.id === passData.value.addressId);
          tableRef.value.setRadioRow(row);
          if (row) selectRow.value = row;
        }
      })
      .finally(() => (loading.value = false));
  };
  const clearRadioRowEevnt = () => {
    tableRef.value.clearRadioRow();
    selectRow.value = null;
  };
  const radioChangeEvent = ({ row }: any) => {
    selectRow.value = row;
  };
  const submitHandle = () => {
    if (!selectRow.value) return message.error("请选择送货仓库!");
    emit("submit", selectRow.value);
  };
  const cancelHandle = () => {
    emit("update:visible", false);
  };
  return {
    searchHandle,
    submitHandle,
    cancelHandle,
    clearRadioRowEevnt,
    radioChangeEvent
  };
}
